package com.maimao.info.controller;

import com.maimao.api.mall.info.AddressApi;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.info.controller.base.BaseController;
import com.maimao.info.service.AddressService;
import com.maimao.model.info.Address;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 收货地址
 *
 * @author MaoLin Wang
 * @date 2021/1/13 2:21 下午
 */
@RestController
public class AddressController extends BaseController implements AddressApi {

    @Autowired
    private AddressService addressService;

    /**
     * 添加收货地址
     *
     * @param address
     * @param bindingResult
     * @return
     */
    @PostMapping("/add")
    @Override
    public Result<?> addAddress(@Valid @RequestBody Address address, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        AuthUser authUser = checkAndGetAuthUser();
        address.setUserId(Long.valueOf(authUser.getId()));
        if (address.getIsDefault() == null) {
            address.setIsDefault(false);
        }
        addressService.add(address);
        return Result.ok();
    }

    /**
     * 更新收货地址
     *
     * @param address
     * @param bindingResult
     * @return
     */
    @PostMapping("/update")
    @Override
    public Result<?> updateAddress(@Valid @RequestBody Address address, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        if (address.getId() == null || address.getId() <= 0) {
            return Result.notValid();
        }
        AuthUser authUser = checkAndGetAuthUser();
        address.setUserId(Long.valueOf(authUser.getId()));
        if (address.getIsDefault() == null) {
            address.setIsDefault(false);
        }
        addressService.update(address);
        return Result.ok();
    }

    @DeleteMapping("/delete")
    @Override
    public Result<?> deleteAddress(@RequestBody Map<String, String> data) {
        if (data == null || data.size() <= 0) {
            return Result.notValid();
        }
        String id = data.get("id");
        if (StringUtils.isEmpty(id)) {
            return Result.notValid();
        }
        Long idLong = Long.valueOf(id);
        if (idLong <= 0) {
            return Result.notValid();
        }
        AuthUser authUser = checkAndGetAuthUser();
        addressService.delete(authUser.getId2Long(), idLong);
        return Result.ok();
    }

    /**
     * 设为默认地址
     */
    @PostMapping("/set-default")
    @Override
    public Result<?> setDefault(@RequestBody Map<String, String> data) {
        if (data == null || data.size() <= 0) {
            return Result.notValid();
        }
        String id = data.get("id");
        if (StringUtils.isEmpty(id)) {
            return Result.notValid();
        }
        Long idLong = Long.valueOf(id);
        if (idLong <= 0) {
            return Result.notValid();
        }
        AuthUser authUser = checkAndGetAuthUser();
        addressService.setDefault(idLong, authUser.getId2Long());
        return Result.ok();
    }

    /**
     * 收货地址列表
     */
    @Override
    @GetMapping("/list/login")
    public Result<?> addressData() {
        AuthUser authUser = checkAndGetAuthUser();
        return Result.okData(addressService.userAddressList(authUser.getId2Long()));
    }

    /**
     * 查询用户默认地址
     *
     * @param userId
     * @return
     */
    @Override
    @GetMapping("/client/default")
    public Result<?> defaultAddress(@RequestParam("userId") Long userId) {
        if (userId == null || userId <= 0) {
            return Result.notValid();
        }
        return Result.okData(addressService.selectDefault(userId));

    }
}
